package com.example.myui.activity;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import com.example.myui.R;
import com.example.myui.fragment.Fragment0;
import com.example.myui.fragment.Fragment1;
import com.example.myui.fragment.Fragment2;
import com.example.myui.fragment.Fragment3;

import java.util.ArrayList;
import java.util.List;

public class LazyloadActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TabLayout tablayout;
    private LazyAdapter lazyAdapter;
    private int mSelectedPosition = 0;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lazyload);
        initView(savedInstanceState);
    }

    private void initView(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mSelectedPosition = savedInstanceState.getInt("key");
        }
        fragments = new ArrayList<>();
        fragments.add(new Fragment0());
        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        viewPager = findViewById(R.id.viewpager);
        tablayout = findViewById(R.id.tablayout);
        tablayout.addTab(tablayout.newTab());
        tablayout.addTab(tablayout.newTab());
        tablayout.addTab(tablayout.newTab());
        tablayout.addTab(tablayout.newTab());

        lazyAdapter = new LazyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(lazyAdapter);
        tablayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(4);
        for (int i = 0; i < tablayout.getChildCount(); i++) {
            TabLayout.Tab tabAt = tablayout.getTabAt(i);
            if (tabAt == null) continue;
            tabAt.setCustomView(lazyAdapter.getTabView(i));
        }

        lazyAdapter.selectedTab(mSelectedPosition);

        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mSelectedPosition = tablayout.getSelectedTabPosition();
                lazyAdapter.selectedTab(mSelectedPosition);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private class LazyAdapter extends FragmentPagerAdapter {
        private int[] tabTextValues = new int[]{
                R.string.wechat, R.string.contacts,
                R.string.discover, R.string.me
        };
        private int[] tabDrawables = new int[]{
                R.drawable.selector_ic_tab_chat, R.drawable.selector_ic_tab_contacts,
                R.drawable.selector_ic_tab_discover, R.drawable.selector_ic_tab_me
        };
        private List<View> tabViews;

        public LazyAdapter(FragmentManager fm) {
            super(fm);
            tabViews = new ArrayList<>();
            for (int i = 0; i < tabDrawables.length; i++) {
                View view = LayoutInflater.from(LazyloadActivity.this).inflate(R.layout.item_lazy_tab, null);
                view.findViewById(R.id.ilt_iv).setBackgroundResource(tabDrawables[i]);
                ((TextView) view.findViewById(R.id.ilt_tv)).setText(getPageTitle(i));
                tabViews.add(view);
            }
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return tabDrawables.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return getString(tabTextValues[position]);
        }

        /**
         * Returns the view of tab item.
         */
        public View getTabView(int position) {
            return tabViews.get(position);
        }

        /**
         * Select tab and update ui.
         */
        public void selectedTab(int position) {
            for (int i = 0; i < tabViews.size(); i++) {
                View view = tabViews.get(i);
                view.findViewById(R.id.ilt_iv).setSelected(false);
                view.findViewById(R.id.ilt_content).setAlpha(0.5f);
                ((TextView) view.findViewById(R.id.ilt_tv))
                        .setTextColor(ContextCompat.getColor(LazyloadActivity.this, R.color.tab_normal));
            }
            View view = tabViews.get(position);
            view.findViewById(R.id.ilt_iv).setSelected(true);
            view.findViewById(R.id.ilt_content).setAlpha(1.0f);
            ((TextView) view.findViewById(R.id.ilt_tv))
                    .setTextColor(ContextCompat.getColor(LazyloadActivity.this, R.color.tab_selected));
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("key", mSelectedPosition);
    }
}
